<?php
class Category extends AppModel{
    public $useTable  = 'category';
    public $primaryKey  = 'category_id';
    public $lang_code = 'vn';
    
    public $validate = array(
        'title_vn' => array(
            'nonEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Vui lòng nhập tên danh mục',
                'allowEmpty' => false
            ),
            'alphaNumericDashUnderscore' => array(
                'rule'    => array('alphaNumericDashUnderscore'),
                'message' => 'Tên danh mục không chứa ký tự đặc biệt'
            ),
        ),
        'title_en' => array(
            'nonEmpty' => array(
                'rule' => array('notEmpty'),
                'message' => 'Vui lòng nhập tên danh mục',
                'allowEmpty' => false
            ),
            'alphaNumericDashUnderscore' => array(
                'rule'    => array('alphaNumericDashUnderscore'),
                'message' => 'Tên danh mục không chứa ký tự đặc biệt'
            ),
        ),
    );

    
    public function alphaNumericDashUnderscore($check) {
        // $data array is passed using the form field name as the key
        // have to extract the value to make the function generic
        $value = array_values($check);
        $value = $value[0];

        return preg_match('/^[a-zA-Z0-9_ \-]*$/', $value);
    }
        
    function add($data = array())
    {
        $this->save($data);
		return $this->getLastInsertId();
    }
    
    public function get_category($status = "")
    {
        $status = $status == "all" ? '' : 'AND category.`status` = 1';
    	$lang_code = $this->lang_code;
        $sql = "SELECT category.*, category_desc.*, image.`name`
				FROM category_desc, category
                LEFT JOIN image ON category.category_id = image.post_id AND image.type = 'category' AND image.`status` = 1
				WHERE category.category_id = category_desc.category_id $status AND category_desc.lang_code = '$lang_code' 
				ORDER BY category.sort_order, category_desc.title";

		return $this->query($sql);
    }

    public function get_category_by_id($category_id = "", $lang = '')
    {
    	if($category_id == "")
    		return null;
    	$lang_code = $this->lang_code;

    	$where_lang = "AND category_desc.lang_code = '$lang_code'";
    	if($lang == 'all')
    		$where_lang = "";

    	$result = array();
    	$sql = "SELECT * FROM category WHERE category_id = '$category_id'";
    	$data = $this->query($sql);
    	if(!empty($data))
    	{
    		$result['category'] = $data[0]['category'];
    	}
    	else
    	{
    		$result['category'] = array();
    	}
    	

    	$sql = "SELECT * 
				FROM  category_desc 
				WHERE category_id = '$category_id' $where_lang ";
		$data = $this->query($sql);
		$category_desc = array();
		foreach ($data as $value) {
			$lang_code = $value['category_desc']['lang_code'];
			$category_desc[$lang_code] = $value['category_desc'];

		}
		$result['category_desc'] = $category_desc;
        
        
        
   		$where_lang = $lang == "all" ? "" : "AND lang_code = '$lang_code'";
            
        $sql = "SELECT * FROM link WHERE type = 'category' AND post_id = '$category_id' $where_lang ";
		$data = $this->query($sql);
		$link = array();
		foreach ($data as $value) {
			$lang_code = $value['link']['lang_code'];
			$link[$lang_code] = $value['link'];

		}
        $result['link'] = $link;
		return $result;
    }

    function edit($category_id = "", $data = array())
    {
		return $this->updateAll(
		    $data, 
		    array('category_id' => $category_id)
		);
    }

    function del($category_id = "")
    {
    	$sql = "DELETE FROM category WHERE category_id = '$category_id'";
    	$this->query($sql);
    	$sql = "DELETE FROM category_desc WHERE category_id = '$category_id'";
    	$this->query($sql);
        $sql = "DELETE FROM image WHERE post_id = '$category_id' AND type = 'category'";
    	$this->query($sql);
        $sql = "DELETE FROM link WHERE post_id = '$category_id' AND type = 'category'";
    	$this->query($sql);
    	return 1;
    }

    function change_status($category_id = "")
    {
    	$sql = "SELECT * FROM category WHERE category_id = '$category_id'";
        $data = $this->query($sql);
        if(!empty($data))
        {
            $status = $data[0]['category']['status'];
            $change_status = $status == 1 ? '0' : '1';
            $sql = "UPDATE category SET status = '$change_status' WHERE category_id = '$category_id'";
            $this->query($sql);
            return $change_status;
        }
        return -1;
    }
    
    public function get_all_category($status = "", $parent_id = "")
    {
        $status = $status == "all" ? '' : 'AND category.`status` = 1';
        $parent = $parent_id == "" ? '' : 'AND category.parent_id = '.$parent_id;
    	$lang_code = $this->lang_code;
        $sql = "SELECT category.*, category_desc.title, image.name
				FROM category_desc, category
                LEFT JOIN image ON category.category_id = image.post_id AND image.type = 'category' AND image.`status` = 1
                WHERE 1=1 $parent $status AND category.`category_id` = category_desc.`category_id` 
                AND category_desc.`lang_code` = '$lang_code'
				ORDER BY category.sort_order";
		$category = $this->query($sql);
        $list_category = array();
        foreach($category as $value)
        {
            $temp = array();
            $temp = $value['category'];
            $temp['title'] = $value['category_desc']['title'];
            $temp['image'] = $value['image']['name'];
            $list_category[] = $temp;
        }
        return $list_category;
    }
    
    function filter_category($arrData, $parent = 0, $level = 0, &$result = array()) {
        if (count($arrData) > 0) {
            foreach ($arrData as $key => $val) {
                if ($parent == $val['parent_id']) {
                    $val['level'] = $level;
                    $result[] = $val;
                    $_parent = $val['category_id'];
                    unset($arrData[$key]);
                    $this->filter_category($arrData, $_parent, $level + 1, $result);
                }
            }
        }
        return $result;
    }
    
    function valid_category_code($category_id = null, $category_code = null)
    {
        $sql = array(
			"conditions"=> array(
									"category_code =" => $category_code,
								)
			);
        $data = $this->find('first',$sql);
        if(empty($data))
            return true;
        else
        {
            $cate_id = $data['Category']['category_id'];
            if($cate_id == $category_id)
                return true;
            else
                return false;
        } 
    }
    
    function get_parent_category(){
    	$lang_code = $this->lang_code;
        $sql = "SELECT category.*, category_desc.title, image.name
				FROM category_desc, category
                LEFT JOIN image ON category.category_id = image.post_id AND image.type = 'category' AND image.`status` = 1
                WHERE parent_id = 0 AND category.`category_id` = category_desc.`category_id` 
                AND category_desc.`lang_code` = '$lang_code'
				ORDER BY category.sort_order";
		$category = $this->query($sql);
        $list_category = array();
        foreach($category as $value)
        {
            $list_category[$value['category']['category_id']] = $value['category_desc']['title'];
        }
        return $list_category;
    }
    
}
